package com.dr.service.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import com.dr.common.pojo.WeChatContent;
import com.dr.common.ution.SecurityKit;
import com.dr.common.ution.StringUtil;
import com.dr.service.WeChatUtilService;
import org.springframework.stereotype.Service;

@Service
public class WeChatUtilServiceImpl implements WeChatUtilService {
	
	private static String createNonceStr() {
        return UUID.randomUUID().toString();
    }
	
	private static String createTimestamp() {
	        return Long.toString(System.currentTimeMillis() / 1000);
	}

	@Override
	public Map<String, String> JsSDKInit(String url) {
		WeChatContent content = WeChatContent.getInstance();
		String appid = content.getAppId();
		String jsapi_ticket = content.getJsTicket().getTicket();
		String timestamp = createTimestamp();
		String nonceStr = createNonceStr();
		if(StringUtil.isEmpty(url)){
			url =  content.getBaseUrl();
		}
		String  signStr = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + nonceStr + "&timestamp=" + timestamp  + "&url=" + url;
		String signature = SecurityKit.sha1(signStr);
		Map<String, String> map = new HashMap<String, String>();
		map.put("appid", appid);
		map.put("timestamp", timestamp);
		map.put("nonceStr", nonceStr);
		map.put("signature", signature);
		return map;
	}
}
